layui.use(['form', 'jquery', 'layer', 'table'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;

	var table_filter = 'attr_list' + category_sign;
	var edit_attr = function(field, value, attr_id) {
		var post = '{"attr_id":"' + attr_id + '","' + field + '":"' + value + '","category_sign":"' + category_sign + '"}';
		var data = JSON.parse(post);
		send_info(attr_save_url, data, function(data) {
			if(data.code == -1) {
				layer.msg(data.msg, {
					icon: 2,
					anim: 6
				});
			} else {
				layer.msg('修改成功', {
					icon: 1
				});
			}
		})
	}
	// 渲染表格
	table.render({
		elem: '#' + table_filter,
		id: table_filter,
		url: attr_list_url,
		method: 'post',
		height: 'auto',
		toolbar: '#attrToolbar',
		defaultToolbar:[],
		cols: [attr_cols]
	});
	//监听编辑
	table.on('edit(' + table_filter + ')', function(obj) {
		var field = obj.field,
			value = obj.value,
			attr_id = obj.data.attr_id;
		edit_attr(field, value, attr_id);
	});
	// 监听是否必填
	form.on('checkbox(is_mustTpl)',function(obj){
		var field = this.name;
		var value = obj.elem.checked?1:2;
		var attr_id = obj.value;
		edit_attr(field, value, attr_id);
		return false;
	});
	// 监听快速上架是否必填
	form.on('checkbox(fast_is_mustTpl)',function(obj){
		var field = this.name;
		var value = obj.elem.checked?1:2;
		var attr_id = obj.value;
		edit_attr(field, value, attr_id);
		return false;
	});
	// 监听类型
	form.on('switch(value_typeTpl)',function(obj){
		var field = this.name;
		var value = obj.elem.checked?1:2;
		var attr_id = obj.value;
		edit_attr(field, value, attr_id);
		return false;
	})
	form.on('switch(select_typeTpl)',function(obj){
		var field = this.name;
		var value = obj.elem.checked?1:2;
		var attr_id = obj.value;
		edit_attr(field, value, attr_id);
		return false;
	})
	// 监听表头按钮
	table.on('toolbar(' + table_filter + ')', function(obj) {
		switch (obj.event){
			case 'addAttr':
				layer.open({
					title: '新增分类属性',
					type: 2,
					content: add_attr_url,
					area: ['800px', '600px']
				});
				break;
			default:
				break;
		}
		return false;
	})
	
	son_reload_table = function() {
		table.reload(table_filter, {
			done: function() {
				//$('input[name="sort"]').focus();
			}
		});
	}
	// 表格工具监听
	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		var attr_id = data.attr_id;
		switch(e) {
			case 'del':
				// 删除
				layer.confirm('确定删除吗？', function(index) {

					send_info(attr_delete_url, {
						attr_id: attr_id
					}, function(data) {
						if(data.code == 0) {
							obj.del();
							layer.msg('删除成功', {
								icon: 1
							});
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				break;
			case 'edit':
				create_tab($(this));
				break;
			case 'update':
				send_info(attr_one_url, {
					attr_id: data.attr_id
				}, function(data) {
					delete data.data.attr_id;
					obj.update(data.data);
				})
				break;
			default:
				break;
		}
	})
})